<?php

namespace app\admin\model\wwh;

use think\Db;
use think\Model;

class Archives extends Model
{
    // 表名
    protected $name = 'wwh_archives';
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'type_text',
        'rec_data_text',
        'lang_text',
        'd_type_text',
        'url',
    ];

    protected static function init()
    {
        $ColumnModel = new Column;

        self::afterInsert(function ($row) use ($ColumnModel) {
            $pk = $row->getPk();
            $updateData = ['weigh' => $row[$pk]];

            if (!empty($row->column_id)) {
                $column = $ColumnModel->find($row['column_id']);
                if ($column) {
                    $updateData['classify'] = $column->classify;
                    $updateData['tpl'] = $column->showtpl;
                }
            }
            Db::name('wwh_archives')->where($pk, $row[$pk])->update($updateData);
        });

        self::beforeInsert(function ($row) use ($ColumnModel) {
            if (!empty($row->column_id)) {
                $column = $ColumnModel->find($row['column_id']);
                if($column) {
                    $row->column_ids = ($column->parent_ids ? $column->parent_ids . ',' : '') . $row->column_id;
                }
            } else {
                $row->column_ids = 0;
            }
        });

        self::beforeUpdate(function ($row) use ($ColumnModel) {
            $changeData = $row->getChangedData();
            if (isset($changeData['column_id'])) {
                $column = $ColumnModel->find($row['column_id']);
                if ($column) {
                    $row->column_ids = ($column->parent_ids ? $column->parent_ids . ',' : '') . $row->column_id;
                    $row->classify = $column->classify;
                    $row->tpl = $column->showtpl;
                } else {
                    $row->column_ids = 0;
                    $row->classify = '';
                    $row->tpl = '';
                }
            }
        });
    }

    public function getUrlAttr($value, $data)
    {
        return $this->buildUrl($value, $data);
    }

    private function buildUrl($value, $data)
    {
        if (empty($data['column_id']) || empty($data['id'])) {
            return '';
        }

        if (!empty($data['classify']) && $data['classify'] == 'download') {
            return addon_url('wwh/column/index', [':diyname' => 'download'], 'html');
        }

        $column = Db::name('wwh_column')->where('id', $data['column_id'])->field('diyname')->find();
        if (!$column) {
            return '';
        }

        return addon_url('wwh/archives/index', [':diyname' => $column['diyname'] ?? '', ':id' => $data['id']], 'html');
    }

    public function getTypeList()
    {
        return ['product' => __('Product'), 'cases' => __('Cases'), 'download' => __('Download'), 'news' => __('News')];
    }

    public function getRecDataList()
    {
        return ['0' => __('Rec_data 0'), '1' => __('Rec_data 1'), '10' => __('Rec_data 10')];
    }

    public function getLangList()
    {
        return ['1' => __('Lang 1'), '2' => __('Lang 2')];
    }

    public function getdTypeList()
    {
        return ['1' => __('用户手册'), '2' => __('产品彩页'), '3' => __('驱动程序'), '4' => __('其他')];
    }

    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : ($data['type'] ?? '');
        $list = $this->getTypeList();
        return $list[$value] ?? '';
    }

    public function getRecDataTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['rec_data']) ? $data['rec_data'] : '');
        $valueArr = explode(',', $value);
        $list = $this->getRecDataList();
        return implode(',', array_intersect_key($list, array_flip($valueArr)));
    }

    public function getLangTextAttr($value, $data)
    {
        $value = $value ?: ($data['lang'] ?? '');
        $list = $this->getLangList();
        return $list[$value] ?? '';
    }

    public function getdTypeTextAttr($value, $data)
    {
        $value = $value ? $value : ($data['d_type'] ?? '');
        $list = $this->getdTypeList();
        return $list[$value] ?? '';
    }

    protected function setRecDataAttr($value)
    {
        return is_array($value) ? implode(',', $value) : $value;
    }

    public function column()
    {
        return $this->hasOne('app\admin\model\wwh\Column', 'id', 'column_id');
    }
}
